/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package imat;

import java.awt.Dimension;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import se.chalmers.ait.dat215.project.Order;
import se.chalmers.ait.dat215.project.ShoppingItem;

/**
 *
 * @author Andreas
 */
public class HistoryItemPresenter {

    private HistoryItemView historyItemView;
    private String name;
    private Order order;

    private int maxHeight = 250;

    public HistoryItemPresenter(HistoryItemView historyItemView) {
        this.historyItemView = historyItemView;
    }

    public void buyOrder() {
        DataHandler.getInstance().addOrderToTrolley(order);
    }

    public void receiptButtonClicked() {
        if (historyItemView.getReceiptPanel().isVisible()) {
            this.historyItemView.getReceiptPanel().setVisible(false);
            this.historyItemView.getShowReceipt().setText("Visa varor");
            this.historyItemView.setPreferredSize(new Dimension(536, 40));
            this.historyItemView.revalidate();
        }
        else {
            this.historyItemView.getReceiptPanel().setVisible(true);
            this.historyItemView.getShowReceipt().setText("Göm varor");
            this.historyItemView.setPreferredSize(new Dimension(536, maxHeight));
            this.historyItemView.revalidate();
        }
    }

    public String fillOut(String in, int n) {
        StringBuilder out = new StringBuilder(in);
        for (int i=0; i<n-in.length(); i++) {
            out.append(' ');
        }
        return out.toString();
    }

    public void setOrder(Order order) {
        this.order = order;
        Date date = order.getDate();
        SimpleDateFormat formatter;

        formatter = new SimpleDateFormat("yy-MM-dd HH:mm", Locale.getDefault());
        //this.name = DateFormat.getInstance().getInstance().getInstance().getInstance().getInstance().format(order.getDate());
        this.name = formatter.format(date);

        DefaultListModel listModel = new DefaultListModel();

        DecimalFormat nf = new DecimalFormat();
        nf.setMinimumFractionDigits(2);

        int totalPrice = 0;
        for (ShoppingItem item : order.getItems()) {
            totalPrice += item.getProduct().getPrice() * item.getAmount();
            listModel.addElement(
                    fillOut(item.getProduct().getName(), 16)+" "+
                    fillOut(item.getAmount()+" "+item.getProduct().getUnitSuffix()+" * "+
                    nf.format(item.getProduct().getPrice()), 32)+" : "+
                    nf.format(item.getTotal())+" kr"
            );
        }
        listModel.addElement(fillOut("", 48)+"  : "+nf.format(totalPrice)+" kr");

        historyItemView.getReceiptList().setModel(listModel);

        historyItemView.getNameLabel().setText(name);
        historyItemView.getCountLabel().setText(Integer.toString(order.getItems().size())+" st");
        historyItemView.getPriceLabel().setText(Double.toString(totalPrice)+" kr");

        maxHeight = 60 + 20*order.getItems().size()+1;

    }



}
